import java.io.StringReader;
import java.util.ArrayList;
import javax.ws.rs.core.MultivaluedMap;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.CharacterData;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;

import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.WebResource;
import com.sun.jersey.core.util.MultivaluedMapImpl;

public class DataLoader {
	
	public ArrayList<Arrival> loadArrivals(int stopID) {
		ArrayList<Arrival> arrivals = new ArrayList<Arrival>();
		
		Client client = Client.create();
		WebResource webResource = client
				.resource("http://api.onebusaway.org/api");
		MultivaluedMap<String, String> queryParams = new MultivaluedMapImpl();
		queryParams.add("key", "e5cceffb-3a4d-4f95-96e6-42fb93d3b792");
		queryParams.add("version", "2");
		String response = webResource.path("where/arrivals-and-departures-for-stop/1_" + stopID + ".xml")
				.queryParams(queryParams).get(String.class);
		//System.out.println(response);
		
		DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
		Document doc = null;
		try {
			DocumentBuilder db = dbf.newDocumentBuilder();
			InputSource is = new InputSource();
			is.setCharacterStream(new StringReader(response));
			doc = db.parse(is);
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		// get data for all arrivalAndDepartures
		NodeList l = doc.getElementsByTagName("arrivalAndDeparture");
		
		for(int i = 0; i < l.getLength(); i++) {
			Element elmt = (Element) l.item(i);
			
			NodeList name = elmt.getElementsByTagName("routeShortName");
			Element line = (Element) name.item(0);
			Node child = line.getFirstChild();
			CharacterData cd = (CharacterData) child;
			String routeID = cd.getData();
			
			NodeList dests = elmt.getElementsByTagName("tripHeadsign");
			line = (Element) dests.item(0);
			child = line.getFirstChild();
			cd = (CharacterData) child;
			String destination = cd.getData();
			
			NodeList predDepTime = elmt.getElementsByTagName("predictedDepartureTime");
			line = (Element) predDepTime.item(0);
			child = line.getFirstChild();
			cd = (CharacterData) child;
			String predictedDepartureTime = cd.getData();
			
			NodeList schedDepTime = elmt.getElementsByTagName("scheduledDepartureTime");
			line = (Element) schedDepTime.item(0);
			child = line.getFirstChild();
			cd = (CharacterData) child;
			String scheduledDepartureTime = cd.getData();
			
			NodeList predBool = elmt.getElementsByTagName("predicted");
			line = (Element) predBool.item(0);
			child = line.getFirstChild();
			cd = (CharacterData) child;
			String predicted = cd.getData();
			//System.out.println(predicted);
			
			arrivals.add(new Arrival(stopID, routeID, destination, predictedDepartureTime, scheduledDepartureTime, predicted));
		
		}
		
		return arrivals;
	}
}
